<template>
  <div>
    <el-checkbox-group v-model="roleIdList">
      <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
    </el-checkbox-group>
    <el-row class="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button type="primary" size="small" @click="onSubmit">确定</el-button>
        <el-button size="small" @click="onCancel">取消</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>
// 获取角色列表接口
import { getRolesAPI } from '@/api/setting'
// 根据id获取角色详情
import { getUserDetailById } from '@/api/user'
// 给角色分配权限
import { assignRolesAPI } from '@/api/employees'
export default {
  name: 'AssignRole',
  props: {
    userId: {
      type: [Number, String],
      required: true
    }
  },
  data() {
    return {
      roleIdList: [], // 角色id列表
      roleList: [] // 角色列表
    }
  },
  created() {
    this.getRolesFn()
    this.getUserDetailByIdFn()
  },
  methods: {
    // 点击确定
    async  onSubmit() {
      await assignRolesAPI({ id: this.userId, roleIds: this.roleIdList })
      this.$emit('close')
    },
    // 点击取消
    onCancel() {
      this.$emit('close')
    },
    // 获取角色列表的方法
    async getRolesFn() {
      const res = await getRolesAPI()
      this.roleList = res.data.rows
    },
    // 根据id获取角色详情方法
    async getUserDetailByIdFn() {
      const res = await getUserDetailById(this.userId)
      this.roleIdList = res.data.roleIds
    }
  }
}
</script>

<style lang="scss" scoped>
.footer {
  margin-top: 30px;
}
</style>
